package com.fmsz.xmly.activitys.broadcast;

import android.content.Intent;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.broadcast.RadioStationAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.entitys.broadcast.ProvinceListEntity;
import com.fmsz.xmly.entitys.broadcast.province.RadioStationEntity;
import com.fmsz.xmly.widget.SlidingFinshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LocalRadioActivity extends AppCompatActivity implements SlidingFinshLayout.slidingFinshListener {

    private String provinceCode;
    private String provinceName;
    public static final int LOCAL = 2;
    public static final int COUNTRY = 1;
    public static final int INTERNET = 3;
    private int tag;
    private int pageNum = 1;
    private ListView listView;
    private RadioStationAdapter adapter;
    private List<RadioStationEntity.ResultEntity> datas;
    private MyApplication application = null;
    private SlidingFinshLayout layout;
    private Toolbar toolbar;
    private ActionBar actionBar;
    private TextView textView;
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_radio);
        Intent intent = getIntent();
        tag = intent.getIntExtra("tag",0);
        initView();
        if (tag==2) {
            provinceName = intent.getStringExtra("provinceName");
            loadLocalData();//判断如果是本地台就加载省市列表并根据provinceName获取provinceCode
        }else{
            loadElseData();//加载其他台数据
        }
    }

    //加载其他台数据
    private void loadElseData() {
        application.getBroadService().getProvinceListData(provinceCode, String.valueOf(tag), pageNum).enqueue(new Callback<RadioStationEntity>() {
            @Override
            public void onResponse(Call<RadioStationEntity> call, Response<RadioStationEntity> response) {
                datas.addAll(response.body().getResult());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<RadioStationEntity> call, Throwable t) {

            }
        });
    }

    //初始化UI
    private void initView() {
        drawer = ((DrawerLayout) findViewById(R.id.drawer));
        drawer.openDrawer(GravityCompat.END);
        application = (MyApplication) getApplication();
        toolbar = ((Toolbar) findViewById(R.id.toolbar_local));
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        textView = ((TextView) toolbar.findViewById(R.id.title_bar));
        textView.setText(tag==1?"国家台":tag==2?"本地台":"网络台");
        datas = new ArrayList<>();
        listView = ((ListView) findViewById(R.id.broad_local_lv));
        adapter = new RadioStationAdapter(this, datas);
        listView.setAdapter(adapter);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, 0, 0) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                LocalRadioActivity.this.finish();
            }
        };
        drawer.addDrawerListener(toggle);
    }

    //本地台数据
    private void loadLocalData() {
        //加载广播省市列表数据
        application.getBroadService().getProvinceList().enqueue(new Callback<ProvinceListEntity>() {
            @Override
            public void onResponse(Call<ProvinceListEntity> call, Response<ProvinceListEntity> response) {
                if (response.body()!=null) {
                    for (int i = 0; i < response.body().getResult().size(); i++) {
                        if (provinceName .equals( response.body().getResult().get(i).getProvinceName())) {
                            provinceCode = response.body().getResult().get(i).getProvinceCode();
                        }
                    }
                    loadElseData();
                }
            }

            @Override
            public void onFailure(Call<ProvinceListEntity> call, Throwable t) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //滑动布局的回调监听(滑动退出activity)
    @Override
    public void onslidingFinsh() {
        finish();
    }
}
